﻿using ConfExpert.Forms.Base;
using ConfExpert.Forms.Depo.Aksesuar;

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevTeam;

using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class AksesuarListesi : XForm
    {
        public AksesuarListesi()
        {
            InitializeComponent();
        }

        BindingSource bsAksesuar, bsMusteriKodlari, bsUreticiKodlari;
        private void AksesuarListesi_Load(object sender, EventArgs e)
        {

            GetDetails("Aksesuar;AksesuarMusteriKodlari;AksesuarUreticiKodlari", "GetAksesuarByUserId", WhoAmI);
            bsAksesuar = new BindingSource(ds, "Aksesuar");
            bsMusteriKodlari = new BindingSource(bsAksesuar, "FK_AksesuarMusteriKodlari_Aksesuar_AksesuarId");
            bsUreticiKodlari = new BindingSource(bsAksesuar, "FK_AksesuarUreticiKodlari_Aksesuar_AksesuarId");

            InitGridView(gvAksesuarMaster, bsAksesuar);
            InitGridView(gvMusteriKodlari, bsMusteriKodlari);
            InitGridView(gvUreticiKodlari, bsUreticiKodlari);

            TAM.AksesuarTableAdapter = new Datasets.MyDataTableAdapters.AksesuarTableAdapter();
            TAM.AksesuarMusteriKodlariTableAdapter = new Datasets.MyDataTableAdapters.AksesuarMusteriKodlariTableAdapter();
            TAM.AksesuarUreticiKodlariTableAdapter = new Datasets.MyDataTableAdapters.AksesuarUreticiKodlariTableAdapter();

            AddBsToList(bsAksesuar, bsMusteriKodlari, bsUreticiKodlari);

            DataTable dtx = ds.Tables["Table3"];
            RepositoryItemGridLookUpEdit gle = GetCustomGLE(dtx, "Brand", "BrandId");
            gvAksesuarMaster.Columns["BrandId"].ColumnEdit = gle;


            gvAksesuarMaster.AddSummary();
            if (ytk(198))
                gvAksesuarMaster.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;

            gvMusteriKodlari.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            gvUreticiKodlari.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;

            //TAM.AksesuarTableAdapter.Fill(ds.Aksesuar);

            RepositoryItem ri = RI(Rp.V_User);
            gvAksesuarMaster.Columns["Ekleyen"].ColumnEdit = gvAksesuarMaster.Columns["Guncelleyen"].ColumnEdit = ri;

            GridManager gm = GridManager.GetManager(gvAksesuarMaster);
            gm.GridMenu.AddMenu("Toplu Resim Göster", Buyut);
            //gm.GridMenu.AddMenu("Aksesuar Notu Ekle", AddAksesuarNote);

            gvAksesuarMaster.DisableRowDelete();
            gvAksesuarMaster.MakeColumnsReadOnly("Ekleyen", "Guncelleyen", "Deleted", "MusteriKodu", "UreticiKodu");

            gvAksesuarMaster.YanYanaGetirSagina("UreticiKodu", "Ekleyen", "Guncelleyen", "Deleted");

            gvAksesuarMaster.MakeColumnInvisible("Lokasyon", "ReorderPoint", "Inactive", "Usable", "A1", "A2", "A3", "A4", "A5");
            gvMusteriKodlari.MakeColumnInvisible("AksesuarMusteriKodlariId", "ScA", "AksesuarId", "InsertedOn");
            gvUreticiKodlari.MakeColumnInvisible("AksesuarUreticiKodlariId", "ScA", "AksesuarId", "InsertedOn");

            btnKaydet.Visibility = ytk(52) ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            btnGuncelle.ItemClick += btnGuncelle_ItemClick;
            ds.Aksesuar.TableNewRow += Aksesuar_TableNewRow;

            gvAksesuarMaster.BestFitColumns();
            gvMusteriKodlari.BestFitColumns();
            gvUreticiKodlari.BestFitColumns();
        }

        void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvAksesuarMaster.EndEditing();
            GetDetails("Aksesuar;AksesuarMusteriKodlari;AksesuarUreticiKodlari", "GetAksesuarByUserId", WhoAmI);
            gvAksesuarMaster.BestFitColumns();
        }

        private void AddAksesuarNote(object sender, EventArgs e)
        {
            string aksIds = gvAksesuarMaster.GetSelectedValues("AksesuarId");
            DataSet ds = (DataSet)bsAksesuar.DataSource;
            DataRow[] drSelected = ds.Tables["Aksesuar"].Select("AksesuarId in (" + aksIds + ") and Inactive = 1");

            if (drSelected.Length > 0)
            {
                string ErrorMessage = "Aşağıdaki aksesuarlar zaten atıl durumdadır. Atıl olan aksesuarları seçmeyin!\n\n";
                foreach (DataRow dr in drSelected)
                {
                    ErrorMessage += dr["StokKod"].ToStr() + "\n";
                }
                ShowErrorMessage(ErrorMessage);
                return;
            }

            if (aksIds != String.Empty)
            {
                int stateId = 0;
                using (AksesuarNotuEkle ane = new AksesuarNotuEkle())
                    if (ane.ShowDialog() == DialogResult.OK)
                    {
                        stateId = ane.isPassive ? 5 : 0;
                        ExecuteNonQuery("InsAksesuarPasif", aksIds, WhoAmI, stateId, ane.isUsable, ane.note, ane.musteriFaturaDurumu, ane.faturaNo);

                        ShowInfoMessage("Seçtiğiniz aksesuarlar onaya gönderildi");
                    }
            }
        }

        private void Buyut(object sender, EventArgs e)
        {
            string idxs = gvAksesuarMaster.GetSelectedValues("AksesuarId");
            using (Resimler rs = new Resimler(idxs))
                rs.ShowDialog();
        }

        void Aksesuar_TableNewRow(object sender, System.Data.DataTableNewRowEventArgs e)
        {
            e.Row["Ekleyen"] = WhoAmI;
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvAksesuarMaster.EndEditing();
            if (ds.Aksesuar.Select("ProductCategoryId is null").Length > 0)
            {
                XtraMessageBox.Show("ProductCategoryId alanı boş olamaz!", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            if (ds.Aksesuar.GetChanges() != null)
            {
                DataRow[] dr = ds.Aksesuar.GetChanges().Select("AksesuarId>0");
                foreach (DataRow drItem in dr)
                {
                    int AksesuarId = drItem["AksesuarId"].ToInt();
                    ds.Aksesuar.FindByAksesuarId(AksesuarId)["Guncelleyen"] = WhoAmI;
                    //drItem["Guncelleyen"] = WhoAmI;
                }
            }
            try
            {
                Save();

                gvAksesuarMaster.BestFitColumns();
            }
            catch (Exception ex)
            {
                if (ex.ToString().Contains("UC_AksesuarConflict"))
                    XtraMessageBox.Show("Eklemek istediğiniz aksesuar sistemde zaten kayıtlı. Değişiklikleriniz KAYDEDİLMEYECEK !", "Aksesuar tekrarı", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private string GetAksesuarIds()
        {
            string idxs = string.Empty;
            foreach (int rowHandle in gvAksesuarMaster.GetSelectedRows())
                if (rowHandle > -1)
                    idxs += gvAksesuarMaster.GetRowCellValue(rowHandle, "AksesuarId").ToString() + ",";

            if (idxs.Substring(idxs.Length - 1, 1) == ",")
                idxs = idxs.Substring(0, idxs.Length - 1);
            return idxs;
        }
    }
}